import express from 'express';
import data from '../data';
import ResMsg from '../common/ResMsg';
const homeRouter = express.Router();

// 首页轮播图
homeRouter.post('/banners', (req, res, next) => {
    const banners = [
        'http://localhost:3000/img/home/banner-01.png',
        'http://localhost:3000/img/home/banner-02.png',
        'http://localhost:3000/img/home/banner-03.png',
    ];
    const resMsg = new ResMsg(true);
    resMsg.banners = banners;
    res.json(resMsg);
});

// 首页广告图：主图，3张拼接广告图
homeRouter.post('/advs', (req, res, next) => {
    const advs = [
        { id: '201', imgUrl: 'http://localhost:3000/img/home/main-adv.png' },
        { id: '301', imgUrl: 'http://localhost:3000/img/home/adv-01.png' },
        { id: '302', imgUrl: 'http://localhost:3000/img/home/adv-02.png' },
        { id: '303', imgUrl: 'http://localhost:3000/img/home/adv-03.png' }
    ];
    const resMsg = new ResMsg(true);
    resMsg.advs = advs;
    res.json(resMsg);
});

// 首页限时秒杀商品列表
homeRouter.post('/flashSale', (req, res, next) => {
    const resMsg = new ResMsg(true);
    resMsg.statTime = '2020-05-01';
    resMsg.endTime = '2020-05-10';
    resMsg.products = [];
    for (const item of data.products) {
        if (item.saleType === 1) {
            resMsg.products.push(item);
        }
    }
    res.json(resMsg);
});

/*
homeRouter.post('/login', (req, res, next) => {
    req.on('data', r => {
        let params = JSON.parse(decodeURIComponent(r));
    });
    console.log('login参数...', req.params);
    res.send('登录测试接口');
    next();
});
*/

export default homeRouter;